package com.alander.nexusgo.user.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alander.nexusgo.common.model.dto.UserListDTO;
import com.alander.nexusgo.common.model.vo.UserInfoVO;
import com.alander.nexusgo.common.response.Result;
import com.alander.nexusgo.framework.web.starter.controller.BaseController;
import com.alander.nexusgo.user.service.UserService;
import com.github.pagehelper.PageInfo;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.alander.nexusgo.framework.mysql.starter.page.PageUtils.getPageInfo;
import static com.alander.nexusgo.framework.mysql.starter.page.PageUtils.startPage;

/**
 * 用户管理
 */
@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 用户列表
     *
     * @return 用户列表
     */
    @GetMapping("/list")
    public PageInfo<UserInfoVO> list(@RequestBody UserListDTO userListDTO) {
        startPage(userListDTO);
        List<UserInfoVO> userList = userService.listUserInfo(userListDTO);
        return getPageInfo(userList);
    }

    /**
     * 新增用户
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody UserInfoVO userInfoVO) {
        return success(userService.save(userInfoVO));
    }

    /**
     * 获取用户信息
     *
     * @param userId 用户id
     * @return 用户信息
     */
    @GetMapping("/getUserInfo")
    public Result<UserInfoVO> getUserInfo(@RequestParam(value = "userId",required = false) String userId) {
        if (StringUtil.isNullOrEmpty(userId)) {
            userId = StpUtil.getLoginIdAsString();
        }
        UserInfoVO userInfoVO = userService.getUserInfo(userId);
        return success(userInfoVO);
    }

    /**
     * 更新用户信息
     *
     * @param userInfoVO 用户信息
     * @return 更新结果
     */
    @PutMapping("/updateUserInfo")
    public Result<Boolean> updateUserInfo(UserInfoVO userInfoVO) {
        Boolean result;
        try{
            result = userService.updateUserInfo(userInfoVO);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
        return success(result);
    }

    /**
     * 更新用户头像
     */
    @PostMapping("/updateAvatar")
    public Result<String> updateAvatar(MultipartFile avatar) {
        // 参数校验
        if (avatar == null || avatar.isEmpty()) {
            return fail("头像文件不能为空");
        }
        String result;
        try {
            result = userService.updateAvatar(avatar);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
        return success(result);
    }

    /**
     * 修改用户密码
     */
    @PutMapping("/updatePassword")
    public Result<Boolean> updatePassword(String oldPassword, String newPassword) {
        return success(userService.updatePassword(oldPassword, newPassword));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/delete/{userId}")
    public Result<Boolean> delete(@PathVariable(value = "userId") Long userId) {
        return success(userService.deleteById(userId));
    }


    /**
     * 状态修改
     */
    @PutMapping("/updateStatus")
    public Result<Boolean> updateStatus(Long userId, Integer status) {
        return success(userService.updateStatus(userId, status));
    }
}